package com.ysucode.controller.oj;

import com.ysucode.annotation.MyLogger;
import com.ysucode.constant.Oj;
import com.ysucode.domain.Problem;
import com.ysucode.entity.Answer;
import com.ysucode.entity.Result;
import com.ysucode.service.oj.ProblemService;
import com.ysucode.utils.CommandUtil;
import com.ysucode.utils.TaskJava;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Slf4j
@Controller
public class ProblemController {
    @Autowired
    private ProblemService problemService;

    //添加一道Java题目
    @MyLogger("添加Java题目")
    @RequestMapping("/addJava")
    @ResponseBody
    public Result addJava(String title, String level, String keyword, String description, String example, String template, String test) {
        //封装Problem对象
        Problem problem = new Problem();
        problem.setTitle(title);
        problem.setLevel(level);
        problem.setKeyword(keyword);
        problem.setDescription(description);
        problem.setExample(example);
        problem.setTemplate(template);
        problem.setTest(test);
        //持久化到本地
        Integer flag = problemService.addJava(problem);
        //响应
        if (flag == 1) { //添加成功
            log.debug(Oj.ADD_JAVA_SUC);
            return new Result(true, Oj.ADD_JAVA_SUC);
        }
        //添加失败
        log.debug(Oj.ADD_JAVA_ERR);
        return new Result(false, Oj.ADD_JAVA_ERR);
    }

    //根据id查询一道Java题目
    @MyLogger("查询Java题目")
    @RequestMapping("/getJavaById/{id}")
    @ResponseBody
    public Result getJavaById(@PathVariable Integer id) {
        Problem problem = problemService.getJavaById(id);
        if (problem == null) { //查询失败
            log.debug(Oj.GET_JAVA_ERR);
            return new Result(false, Oj.GET_JAVA_ERR);
        }
        //查询成功
        log.debug(Oj.GET_JAVA_SUC);
        return new Result(true, Oj.GET_JAVA_SUC, problem);
    }

    //根据题目id测试题目是否正确
    @MyLogger("提交Java题目")
    @RequestMapping("/testJavaById/{id}")
    @ResponseBody
    public Result testJavaById(String code, @PathVariable Integer id) throws IOException, InterruptedException {
        //根据id查询problem
        Problem problem = problemService.getJavaById(id);
        //把code和template合并起来编译
        Integer result = TaskJava.TestJava(problem, code);
        //判断答案是否正确
        if (result == Oj.COMPILE_ERR) { //编译错误
            //获取编译错误的信息
            String compileErr = CommandUtil.readCompileTxt();
            log.debug(Oj.COMPILE_ERR_MSG);
            return new Result(false, Oj.COMPILE_ERR_MSG, new Answer(result, compileErr));
        } else if (result == Oj.RUNTIME_ERR) { //运行错误
            log.debug(Oj.RUNTIME_ERR_MSG);
            return new Result(false, Oj.RUNTIME_ERR_MSG, result);
        } else if (result == Oj.ANSWER_ERR) { //答案错误
            log.debug(Oj.ANSWER_ERR_MSG);
            return new Result(false, Oj.ANSWER_ERR_MSG, result);
        } else if (result == Oj.ANSWER_SUC) { //答案正确
            log.debug(Oj.ANSWER_SUC_MSG);
            return new Result(true, Oj.ANSWER_SUC_MSG, result);
        } else { //未知错误
            log.debug(Oj.UNKNOWN_MSG);
            return new Result(false, Oj.UNKNOWN_MSG, result);
        }

    }


    @RequestMapping("/ojHome")
    public String ojHome() {
        return "/ojHome";
    }

    @RequestMapping("/submitOj")
    public String submitOj() {
        return "/submitOj";
    }

    @RequestMapping("/registerOj")
    public String registerOj() {
        return "/registerOj";
    }
}
